{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/bin/bash\n",
    "# 查找脚本所在路径，并进入\n",
    "#DIR=\"$( cd \"$( dirname \"$0\"  )\" && pwd  )\"\n",
    "DIR=$PWD\n",
    "cd $DIR\n",
    "echo current dir is $PWD\n",
    "\n",
    "# 设置目录，避免module找不到的问题\n",
    "export PYTHONPATH=$PYTHONPATH:$DIR:$DIR/slim:$DIR/object_detection\n",
    "\n",
    "# 定义各目录\n",
    "output_dir=/output  # 训练目录\n",
    "dataset_dir=/data/weixin-39265957/quiz-w8-data #拿别人上传的数据集用\n",
    "\n",
    "train_dir=$output_dir/train\n",
    "checkpoint_dir=$train_dir\n",
    "eval_dir=$output_dir/eval\n",
    "\n",
    "# config文件\n",
    "config=ssd_mobilenet_v1_pets.config\n",
    "pipeline_config_path=$output_dir/$config\n",
    "\n",
    "# 先清空输出目录，本地运行会有效果，tinymind上运行这一行没有任何效果\n",
    "rm -rvf $output_dir/*\n",
    "\n",
    "# 因为dataset里面的东西是不允许修改的，所以这里要把config文件复制一份到输出目录\n",
    "cp $DIR/$config $pipeline_config_path\n",
    "python ./dataset_path.py\n",
    "for i in {0..4}  # for循环中的代码执行5此，这里的左右边界都包含，也就是一共训练500个step，每100step验证一次\n",
    "do\n",
    "    echo \"############\" $i \"runnning #################\"\n",
    "    last=$[$i*100]\n",
    "    current=$[($i+1)*100]\n",
    "    sed -i \"s/^  num_steps: $last$/  num_steps: $current/g\" $pipeline_config_path  # 通过num_steps控制一次训练最多100step\n",
    "\n",
    "    echo \"############\" $i \"training #################\"\n",
    "    python ./object_detection/train.py --train_dir=$train_dir --pipeline_config_path=$pipeline_config_path\n",
    "\n",
    "    echo \"############\" $i \"evaluating, this takes a long while #################\"\n",
    "    python ./object_detection/eval.py --checkpoint_dir=$checkpoint_dir --eval_dir=$eval_dir --pipeline_config_path=$pipeline_config_path\n",
    "done\n",
    "\n",
    "# 导出模型\n",
    "python ./object_detection/export_inference_graph.py --input_type image_tensor --pipeline_config_path $pipeline_config_path --trained_checkpoint_prefix $train_dir/model.ckpt-$current  --output_directory $output_dir/exported_graphs\n",
    "\n",
    "# 在test.jpg上验证导出的模型\n",
    "python ./inference.py --output_dir=$output_dir --dataset_dir=$dataset_dir\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
